//
//  ClassificationViewController.m
//  MyItem
//
//  Created by admin on 15/12/23.
//  Copyright © 2015年 admin. All rights reserved.
//

#import "ClassificationViewController.h"
#import "ClassTabelView.h"
#import "ClassCollectionView.h"
#import "AFNetworking.h"
#import "wareModel.h"
@interface ClassificationViewController ()
{    
    NSArray *_dataArray;
    
    NSMutableArray *_nameArray;
    
    NSMutableArray *_modelArray;
    
}
@end

@implementation ClassificationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self _createView];
    
    //post请求数据
    [self postSession];

    
}

-(void)_createView
{
    _tableView = [[ClassTabelView alloc]initWithFrame:CGRectMake(0, 64, HScreenWidth/4, HScreenHeight-64-49) style:UITableViewStylePlain];
    _tableView.separatorColor = [UIColor whiteColor];
    _tableView.backgroundColor = [UIColor lightGrayColor];
    NSIndexPath *index = [NSIndexPath indexPathForRow:0 inSection:0];
    [_tableView selectRowAtIndexPath:index animated:NO scrollPosition:UITableViewScrollPositionNone];
    [self.view addSubview:_tableView];
    
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake(90, 90);
    layout.minimumInteritemSpacing = 10;
    layout.minimumLineSpacing = 20;
    layout.headerReferenceSize = CGSizeMake(HScreenWidth/4*3, 100);
    
    _collectionView = [[ClassCollectionView alloc]initWithFrame:CGRectMake(HScreenWidth/4, 64, HScreenWidth/4*3, HScreenHeight-64-49) collectionViewLayout:layout];
    
    [self.view addSubview:_collectionView];
    
}


-(void)postSession
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    //（1）普通POST请求
    NSString *urlString = @"http://portal.m.jd.com/client.action?functionId=commonCatalogs"; //
    NSString *urlStr = @"http://portal.m.jd.com/client.action?functionId=getCmsPromotionsListByCatelogyID"; //头视图
    //潮流女装
//    http://portal.m.jd.com/client.action?functionId=getCmsPromotionsListByCatelogyID
//    {
//        "sign": "IjCM4ypAsZEAWfrer65P9w",
//        "d_model": "iPhone4,1",
//        "networkType": "wifi",
//        "adid": "5BAD0863-7735-475F-B800-07AF74862C1F",
//        "st": "1451205912910",
//        "body": "{\"level\":\"1\",\"catelogyID\":\"100001851\"}",
//        "osVersion": "7.1.2",
//        "d_brand": "apple",
//        "client": "apple",
//        "screen": "640*960",
//        "sv": "1",
//        "area": "15_1213_3038_0",
//        "build": "35977",
//        "uuid": "hjudwgohxzVu96krv/T6Hg==",
//        "clientVersion": "4.4.3",
//        "openudid": "a1e609be3483012e09542a204a2a6dfd05c016be",
//        "partner": "apple"
//    }
    
    NSDictionary *parameters = @{
                                 @"sign": @"CQEXWEBKQ7TMDqSk_KAtUQ",
                                 @"d_model": @"iPhone4,1",
                                 @"networkType": @"wifi",
                                 @"adid": @"5BAD0863-7735-475F-B800-07AF74862C1F",
                                 @"st": @"1451196439830",
                                 @"body": @"{\"level\":\"1\",\"catelogyID\":\"-1\"}",
                                 @"osVersion": @"7.1.2",
                                 @"d_brand": @"apple",
                                 @"client": @"apple",
                                 @"screen": @"640*960",
                                 @"sv": @"1",
                                 @"area": @"15_1213_3038_0",
                                 @"build": @"35977",
                                 @"uuid": @"hjudwgohxzVu96krv/T6Hg==",
                                 @"clientVersion": @"4.4.3",
                                 @"openudid": @"a1e609be3483012e09542a204a2a6dfd05c016be",
                                 @"partner": @"apple"
                                 };
    
    NSDictionary *parameters1 = @{
                                 @"sign": @"Ew5GMcu8J3E9ZLqjs4IMRQ",
                                 @"d_model": @"iPhone4,1",
                                 @"networkType": @"wifi",
                                 @"adid": @"5BAD0863-7735-475F-B800-07AF74862C1F",
                                 @"st": @"1451110674264",
                                 @"body": @"{\"method\":\"bp.category\",\"guid\":\"a1e609be3483012e09542a204a2a6dfd05c016be\",\"uid\":\"\",\"clear\":0,\"token\":\"f9715d6d28c7c4bb51ec5b757cb809f9\"}",
                                 @"osVersion": @"7.1.2",
                                 @"d_brand": @"apple",
                                 @"client": @"apple",
                                 @"screen": @"640*960",
                                 @"sv": @"1",
                                 @"area": @"15_1213_3038_0",
                                 @"build": @"35977",
                                 @"uuid": @"hjudwgohxzVu96krv/T6Hg==",
                                 @"clientVersion": @"4.4.3",
                                 @"openudid": @"a1e609be3483012e09542a204a2a6dfd05c016be",
                                 @"partner": @"apple"
                                 };
    
    [manager POST:urlStr parameters:parameters success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
//         NSLog(@"JSON: %@", responseObject);
        NSArray *cmsPromotionsList = responseObject[@"cmsPromotionsList"];
        NSDictionary *dic = cmsPromotionsList[0];
        
        _collectionView.headViewUrl = dic[@"imageUrl"];
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
    }];
    
    [manager POST:urlString parameters:parameters1 success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject)
     {
//         NSLog(@"responseHeader:%@", task.response);
//         NSLog(@"JSON: %@", responseObject);
         NSArray *data = responseObject[@"data"];
         for (NSDictionary *dataDic in data)
         {
             //         NSDictionary *dataDic = data[0];
             NSString *name = dataDic[@"name"];
             NSArray *catelogyList = dataDic[@"catelogyList"];
             
             _nameArray = [NSMutableArray arrayWithObject:name];

             _modelArray = [NSMutableArray array];
             for (NSDictionary *dic in catelogyList)
             {
                 wareModel *model = [[wareModel alloc]initWithDataDic:dic];
                 
                 [_modelArray addObject:model];
             }
             
         }
         
         _dataArray = _modelArray;
         
         _collectionView.dataArray = _dataArray;
         _collectionView.nameArray = _nameArray;
         [_collectionView reloadData];

         
     } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
         NSLog(@"PostError: %@", error);
     }];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
